using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay.Data;
using UnityEngine;
using UnityEngine.EventSystems;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_PortableItemEntrance : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_PortableItemEntrance
        {
            #region ObjectBinding Generate

            public ExText num { protected set; get; }
            public ExImage icon { protected set; get; }
            public GameObject item { protected set; get; }
            public EventTriggerListener listener { protected set; get; }
            public GameObject numBg { protected set; get; }

            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<ExText>("num", out var __tbv0);
                num = __tbv0;
                __binding.TryGetVariableValue<ExImage>("icon", out var __tbv1);
                icon = __tbv1;
                __binding.TryGetVariableValue<GameObject>("item", out var __tbv2);
                item = __tbv2;
                __binding.TryGetVariableValue<EventTriggerListener>("listener", out var __tbv3);
                listener = __tbv3;
                __binding.TryGetVariableValue<GameObject>("numBg", out var __tbv4);
                numBg = __tbv4;
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify


        protected UIB_UIN_PortableItemEntrance ui { get; set; }
        private UIMsgDispatcher currMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;


        private ItemData _curPortableItemData;

        private const float PressTimeThreshold = 0.5f;

        private bool _pressStart;
        private float _pressedTimeLeft;

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_PortableItemEntrance();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            _curPortableItemData = ItemModule.Instance.GetItemById(GameDataCenter.PlayerData.portableItemId);
            ui.listener.onClick += OnBtnPortableItemClick;
            ui.listener.onDown += OnBtnPortableItemDown;
            ui.listener.onUp += OnBtnPortableItemUp;
            isUpdate = true;
            Refresh();
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void AddMsgListeners()
        {
            currMsgDispatcher.AddListener<int>(MsgEventType.OneItemChanged, OnOneItemChanged);
            currMsgDispatcher.AddListener<long>(MsgEventType.PortableItemSwitch, OnPortableItemSwitch);
        }

        private void OnOneItemChanged(int itemCid)
        {
            if (_curPortableItemData != null && itemCid != _curPortableItemData.cid)
            {
                return;
            }

            Refresh();
        }

        private void OnBtnPortableItemDown(GameObject go, PointerEventData eventdata)
        {
            _pressStart = true;
            _pressedTimeLeft = PressTimeThreshold;
        }

        protected override void OnUpdate(float dt, float unscaledDt)
        {
            if (!_pressStart)
            {
                return;
            }

            _pressedTimeLeft -= dt;
            if (_pressedTimeLeft < 0)
            {
                _pressStart = false;
                ShowUIPortableItem();
            }
        }

        private void OnBtnPortableItemUp(GameObject go, PointerEventData eventdata)
        {
            _pressStart = false;
        }

        private void ShowUIPortableItem()
        {
            //判断一下关卡是否可以切换道具
            if (GameplayAreaManager.Instance.curArea.areaType == GameplayAreaType.Level && LevelPlayUtil.GetDynLevelPlayer().isQuickBarDisabled)
            {
                NoticeModule.ShowNoticeNoCall(500012);
                return;
            }

            var uiData = UD_PortableItem.Create();
            uiData.UpdatePortableItems(_curPortableItemData?.id ?? 0);
            if (uiData.portableItems.Count == 0)
            {
                NoticeModule.ShowNoticeNoCall(20011);
                return;
            }

            OpenUI<UIPortableItem>(uiData);
        }

        private void OnBtnPortableItemClick(GameObject go, PointerEventData eventData)
        {
            if (_pressedTimeLeft < 0)
            {
                return;
            }

            UsePortableItem().Forget();
        }

        private async UniTaskVoid UsePortableItem()
        {
            if (_curPortableItemData == null || _curPortableItemData.num == 0)
            {
                return;
            }
            var result = await ItemModule.CS_useItem(_curPortableItemData.id, _curPortableItemData.cid);
            if (result.IsSuccess)
            {
                ItemModule.Instance.UpItemCdInfo(result.result.itemCdInfo);
            }
        }

        private void OnPortableItemSwitch(long itemId)
        {
            _curPortableItemData = ItemModule.Instance.GetItemById(itemId);
            Refresh();
        }

        private void Refresh()
        {
            if (_curPortableItemData == null || _curPortableItemData.num == 0)
            {
                _curPortableItemData = null;
                ui.item.SetActive(false);
                return;
            }

            ui.item.SetActive(true);
            SetImageSprite(ui.icon, _curPortableItemData.cfgData.Icon_fullPath);
            ui.numBg.SetActive(_curPortableItemData.num > 1);
            ui.num.text = _curPortableItemData.num.ToString();
        }

        protected override void OnDispose()
        {
        }
    }
}
